<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2019/12/18
// +----------------------------------------------------------------------

namespace app\index\controller\api;

use app\common\constants\ApiConstant;
use think\App;
use think\Db;

/**
 *
 * Class Business
 * @package app\index\controller\api
 * @author feraner<feraner@163.com>
 * @date 2019/12/18
 */
class Business extends ApiBase
{

    public function __construct(App $app = null)
    {
        parent::__construct($app);
    }


    /**
     * 获取商家信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function businessInfo()
    {
        $businessId = $this->request->param('businessId', 0);
        if ($businessId) {
            $businessInfo = Db::name('user_extend_bus')
                ->where('user_id', $businessId)
                ->find() ?: [];
            if ($businessInfo) {
                $this->response($businessInfo);
            } else {
                $this->response(
                    [],
                    ApiConstant::API_ERROR,
                    ApiConstant::API_ERROR_STR
                );
            }
        } else {
            $this->response(
                [],
                ApiConstant::API_PARAMETER_ERROR,
                ApiConstant::API_PARAMETER_ERROR_STR
            );
        }
    }







    /**
     * 商企列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function businessList()
    {
        $page = $this->request->param('page', 1);
        $pageSize = $this->request->param('pageSize', 8);

        $businessList = Db::name('user')->alias('u')
            ->where('u.is_deleted', 0)
            ->where('u.status', 2)
            ->where('u.type', 2)
            ->join('user_extend_bus ueb', 'u.id = ueb.user_id')
            ->where('ueb.company_logo', '<>', '')
            ->where('ueb.company_name', '<>', '')
            ->join('user_extend ue', 'ue.user_id = u.id')
            ->where('ue.publish_job_num', '<>', 0)
            ->order('ueb.is_recomend', 'DESC')
            ->page($page, $pageSize)
            ->field('ueb.*,ue.publish_job_num as publishJobNum')
            ->select() ?: [];

        $this->response($businessList);
    }

}
